﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.Assert;
using CommunitySystem.Ctrls;
using CommunitySystem.PublicOfficialSelectionService;
using CommunitySystem.Views;
using EHR.Common;

namespace CommunitySystem
{
    public partial class ShenPiControl2 : AsyncCtrl, IReturnListenerc
    {
        #region IReturnListenerc 成员
        public void BeginReturn()
        {
            Loaded();
        }
        #endregion
        public NavMoveSwitch Nav { get; set; }
        private PublicOfficialSelectionServiceClient Client = new PublicOfficialSelectionServiceClient();
        public ObservableCollection<RenQianTaoLun> models = new ObservableCollection<RenQianTaoLun>();
        public int OrgID = 0;
        private string OrgName = "";
        private int _XuQiuCount { get; set; }
        private bool _isls = false;
        public ShenPiControl2(int orgID, string orgName, int XuQiuCount, bool isls)
        {
            InitializeComponent();
            _isls = isls;
            _XuQiuCount = XuQiuCount;
            OrgID = orgID;
            OrgName = orgName;
            if (Session.LoginUser.LoginName == "admin")
            {
                OKButton.Visibility = System.Windows.Visibility.Collapsed;
                scrol.IsEnabled = false;
            }
            else
            {
                OKButton.Visibility = System.Windows.Visibility.Visible;
                next.IsEnabled = false;
                scrol.IsEnabled = true;
            }
            Loaded();
        }
        public void Loaded()
        {
            Client.GetRenQianTaoLunCompleted -= new EventHandler<GetRenQianTaoLunCompletedEventArgs>(Client_GetRenQianTaoLunCompleted);
            Client.GetRenQianTaoLunCompleted += new EventHandler<GetRenQianTaoLunCompletedEventArgs>(Client_GetRenQianTaoLunCompleted);
            Client.GetRenQianTaoLunAsync(OrgID);
        }
        ObservableCollection<RenQianTaoLun> GetRenQianTaoLun = null;
        void Client_GetRenQianTaoLunCompleted(object sender, GetRenQianTaoLunCompletedEventArgs e)
        {
            stackpanel.Children.Clear();
            GetRenQianTaoLun = e.Result;
            if (e.Result != null && e.Result.Count > 0)
            {
                OKButton.IsEnabled = true;
                if (e.Result[0].ID != 0) next.IsEnabled = true;
                models = e.Result;

                TextBlock tb1 = new TextBlock();
                tb1.Text = "            经党（工）委（组）";
                tb1.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                tb1.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                #region
                //TextBox t = new TextBox();
                //if (!string.IsNullOrEmpty(e.Result[0].SNian)) t.Text = e.Result[0].SNian;
                //t.TextChanged += new TextChangedEventHandler(t_TextChanged);
                //t.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                //t.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                //t.Width = 60;
                //t.Height = 25;
                //Binding b1 = new Binding();
                //b1.Source = e.Result[0];
                //b1.Path = new PropertyPath("SNian");
                //b1.Mode = BindingMode.TwoWay;
               // BindingOperations.SetBinding(t, TextBox.TextProperty, b1);
                
                //TextBlock tb2 = new TextBlock();
                //tb2.Text = "年";
                //tb2.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                //tb2.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;

                //TextBox t1 = new TextBox();
                //if (!string.IsNullOrEmpty(e.Result[0].SYUe)) t1.Text = e.Result[0].SYUe;
                //t1.TextChanged += new TextChangedEventHandler(t_TextChanged);
                //t1.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                //t1.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                //t1.Width = 60;
                //t1.Height = 25;
                //Binding b2 = new Binding();
                //b2.Source = e.Result[0];
                //b2.Path = new PropertyPath("SYUe");
                //b2.Mode = BindingMode.TwoWay;
                //BindingOperations.SetBinding(t1, TextBox.TextProperty, b2);
                
                //TextBlock tb3 = new TextBlock();
                //tb3.Text = "月";
                //tb3.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                //tb3.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;

                //TextBox t2 = new TextBox();
                //if (!string.IsNullOrEmpty(e.Result[0].SRi)) t2.Text = e.Result[0].SRi;
                //t2.TextChanged += new TextChangedEventHandler(t_TextChanged);
                //t2.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                //t2.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                //t2.Width = 60;
                //t2.Height = 25;
                //Binding b3 = new Binding();
                //b3.Source = e.Result[0];
                //b3.Path = new PropertyPath("SRi");
                //b3.Mode = BindingMode.TwoWay;
                //BindingOperations.SetBinding(t2, TextBox.TextProperty, b3);
                #endregion

                DatePicker dp = new DatePicker();

                if (!string.IsNullOrEmpty(e.Result[0].TYDate.ToString())) dp.SelectedDate = e.Result[0].TYDate;
                dp.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                dp.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
               
                Binding b3 = new Binding();
                b3.Source = e.Result[0];
                b3.Path = new PropertyPath("TYDate");
                b3.Mode = BindingMode.TwoWay;
                BindingOperations.SetBinding(dp, DatePicker.SelectedDateProperty, b3);
                //dp.SetBinding(DatePicker.SelectedDateProperty, new System.Windows.Data.Binding("TYDate")
                //{
                //    Mode = System.Windows.Data.BindingMode.TwoWay,
                //    StringFormat = "yyyy-MM-dd"
                //});



                TextBlock tb4 = new TextBlock();
                tb4.Text = "日会议讨论决定如下： ";
                tb4.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                tb4.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;

                StackPanel s = new StackPanel();
                s.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                
                s.Orientation = Orientation.Horizontal;
                s.Children.Add(tb1);
                //s.Children.Add(t);
                //s.Children.Add(tb2); 
                //s.Children.Add(t1); 
                //s.Children.Add(tb3); 
                //s.Children.Add(t2); 
                s.Children.Add(dp);
                s.Children.Add(tb4);

                stackpanel.Children.Add(s);

                int count = ((ObservableCollection<RenQianTaoLun>)e.Result).Count;
                for (int i = 0; i < count; i++)
                {
                    TextBlock tb = new TextBlock();
                    tb.Margin = new Thickness(120, 3, 3, 3);
                    tb.Text = "拟";
                    tb.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    tb.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;

                    ComboBox cb = new ComboBox();
                    cb.Tag = e.Result[i];
                    cb.SelectionChanged += new SelectionChangedEventHandler(cb_SelectionChanged);
                    cb.Width = 90;
                    cb.Height = 25;
                    ComboBoxItem ci = new ComboBoxItem();
                    ci.Content = "同意";
                    ComboBoxItem ci1 = new ComboBoxItem();
                    ci1.Content = "不同意";
                    cb.Items.Add(ci);
                    cb.Items.Add(ci1);

                    if (!string.IsNullOrEmpty(e.Result[i].IsTongYi))
                    {
                        if (e.Result[i].IsTongYi == "同意")
                        {
                            cb.SelectedIndex = 0;
                        }
                        else
                        {
                            cb.SelectedIndex = 1;
                        }
                    }
                    cb.Margin = new Thickness(3, 3, 3, 3);
                    //Binding b = new Binding();
                    //b.Source = e.Result[i];
                    //b.Path = new PropertyPath("IsTongYi");
                    //b.Mode = BindingMode.TwoWay;
                    //BindingOperations.SetBinding(cb, ComboBox.SelectedItemProper, b);

                    TextBlock tb11 = new TextBlock();
                    tb11.Margin = new Thickness(3, 3, 3, 3);
                    tb11.Text = e.Result[i].EmpName;
                    tb11.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    tb11.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;


                    TextBlock tb1s = new TextBlock();
                    tb1s.Margin = new Thickness(3, 3, 3, 3);
                    tb1s.Text = "转任";
                    tb1s.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    tb1s.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;

                    TextBlock tbs = new TextBlock();
                    if (!string.IsNullOrEmpty(e.Result[i].Duty)) tbs.Text =e.Result[i].Department_Duty+" "+ e.Result[i].Duty;
                    tbs.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    tbs.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    tbs.Margin = new Thickness(3, 3, 3, 3);


                    StackPanel sp = new StackPanel();
                    sp.Orientation = Orientation.Horizontal;
                    sp.Margin = new Thickness(3, 3, 3, 3);
                    sp.Children.Add(tb);
                    sp.Children.Add(cb);
                    sp.Children.Add(tb11);
                    sp.Children.Add(tb1s);
                    sp.Children.Add(tbs);

                    stackpanel.Children.Add(sp);
                }

                TextBlock tb5 = new TextBlock();
                tb5.Text = "    特此证明";
                tb5.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                tb5.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                stackpanel.Children.Add(tb5);
            }
            else OKButton.IsEnabled = false;
        }

        private string pattern = @"^[0-9]+([.]{1}[0-9]+){0,1}$";
        void t_TextChanged(object sender, TextChangedEventArgs e)
        {
            Match m = Regex.Match(((TextBox)sender).Text, pattern);   // 匹配正则表达式
            if (!m.Success)   // 输入的不是数字
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("只能输入数字！");
                ((TextBox)sender).Text = "";
                return;
            }
        }

        void cb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ((RenQianTaoLun) ((ComboBox) sender).Tag).IsTongYi = ((System.Windows.Controls.ContentControl)
                                                                  (((ComboBoxItem) ((ComboBox) sender).SelectedItem))).
                Content.
                ToString();
        }
        private int c = 0;
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (Validate())
            {
                if (MessageBox.Show("确定上报会议结果吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    var detail = models.Where(c => c.TYDate != null).FirstOrDefault();
                    foreach (RenQianTaoLun i in models)
                    {
                        i.TYDate = detail.TYDate;
                        Client.InsertRenQianTaoLunCompleted -= new EventHandler<InsertRenQianTaoLunCompletedEventArgs>(Client_InsertRenQianTaoLunCompleted);
                        Client.InsertRenQianTaoLunCompleted += new EventHandler<InsertRenQianTaoLunCompletedEventArgs>(Client_InsertRenQianTaoLunCompleted);
                        Client.InsertRenQianTaoLunAsync(i);
                        c += 1;
                    }
                }
            }
        }
        private bool Validate()
        {
            var detail = models.Where(c => c.TYDate != null).ToList();
            if (detail != null&&detail.Count<=0)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("请选择会议讨论日期!");
                return false;
            }

           var detail1 = models.Where(c => string.IsNullOrEmpty(c.IsTongYi)).FirstOrDefault();
           if (detail1 != null)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("请选择" + detail1.EmpName + "同志讨论会议是否同意!");
                return false;
            }

            //foreach (var item in GetRenQianTaoLun)
            //{
                var tysun = models.Where(c => c.IsTongYi == "同意").ToList();
                if (tysun != null && tysun.Count() > _XuQiuCount)   //同意数只能小于或等于需求数
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("用人单位的需求职位数为" + _XuQiuCount + "个,同意数只能小于或等于需求数!");
                    return false;
                }
            //}
           
          
            return true;
        }

        void Client_InsertRenQianTaoLunCompleted(object sender, InsertRenQianTaoLunCompletedEventArgs e)
        {
            if (c == models.Count)
            {
                c = c - 1;
                ShenPiControl3 view = new ShenPiControl3(Convert.ToInt32(OrgID), OrgName, models,_isls);
                view.Nav = Nav;
                Nav.ShowNextPage(OrgName + "-公示情况", view, this);
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {

            ShenPiControl3 view = new ShenPiControl3(Convert.ToInt32(OrgID), OrgName, models, _isls);
            view.Nav = Nav;
            Nav.ShowNextPage(OrgName + "-公示情况", view, this);
        }
    }
}
